﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

using msn = XihSolutions.DotMSN;
using IM.Interfaces.Protocols;
using IM.Interfaces;
using IM.DataTypes;

namespace IM.Protocols.MSN
{
    public class MSN : IProtocol
    {
        private msn.Messenger messenger = new msn.Messenger();

        Dictionary<User, msn.Contact> userToContacts = new Dictionary<User, XihSolutions.DotMSN.Contact>();
        Dictionary<String, User> stringToUser = new Dictionary<String, User>();

        public String AccountName
        {
            get;
            set;
        }

        public bool Enabled
        {
            get;
            set;
        }

        public String ProtocolName
        {
            get { return "MSN"; }
        }

        public MSN(String accountName)
        {
            this.AccountName = accountName;
            messenger.Nameserver.SignedIn += new EventHandler(Nameserver_SignedIn);
            messenger.Nameserver.SignedOff += new msn.SignedOffEventHandler(Nameserver_SignedOff);
            messenger.ConversationCreated += new msn.ConversationCreatedEventHandler(messenger_ConversationCreated);
        }

        void messenger_ConversationCreated(object sender, msn.ConversationCreatedEventArgs e)
        {
            e.Conversation.Switchboard.TextMessageReceived += new msn.TextMessageReceivedEventHandler(Switchboard_TextMessageReceived);
        }

        void Switchboard_TextMessageReceived(object sender, msn.TextMessageEventArgs e)
        {
            if (OnMessage != null)
            {
                OnMessage(this, this.stringToUser[e.Sender.Name], e.Message.Text);
            }
        }

        void Nameserver_SignedOff(object sender, msn.SignedOffEventArgs e)
        {
            if (OnClose != null)
                OnClose(this);
        }

        void Nameserver_SignedIn(object sender, EventArgs e)
        {
            if (OnLogin != null)
                OnLogin(this);

            if (OnContactListLoad != null)
            {
                OnContactListLoad(this, Users);
            }

            messenger.Owner.Status = msn.PresenceStatus.Online;
        }
        
        #region IProtocol Members

        public event ObjectHandler OnLogin;

        public event ErrorHandler OnError;

        public event MessageHandler OnMessage;

        public event ObjectHandler OnClose;

        public event ContactListHandler OnContactListLoad;

        public User[] Users
        {
            get 
            {
                userToContacts.Clear();
                List<User> users = new List<User>();
                foreach (msn.Contact contact in messenger.ContactList.All)
                {
                    User user = new User(contact.Mail, this);
                    users.Add(user);
                    userToContacts.Add(user, contact);
                }

                return users.ToArray();
            }
        }

        public void Login(string username, string password)
        {
            if (messenger.Connected)
            {
                messenger.Disconnect();
            }
            
            //msn.Settings.TraceSwitch.Level = System.Diagnostics.TraceLevel.Verbose;
            
            messenger.Credentials.ClientID = "msmsgs@msnmsgr.com";
            messenger.Credentials.ClientCode = "Q1P7W2E4J9R8U3S5";
            messenger.Credentials.Account = username;
            messenger.Credentials.Password = password;
            messenger.Connect();
        }

        public void Logout()
        {
            if (messenger.Connected)
                try
                {
                    messenger.Disconnect();
                }
                catch { }
        }

        public void SendMessage(string message, User username)
        {
            

        }

        public event ConnectionStateHandler OnConnectionStateChanged;

        public void LoadSettingsData(object obj)
        {
            SettingsData data = obj as SettingsData;
            this.Login(data.Login, data.Password);

        }

        public User UserID
        {
            get { return new User(messenger.Owner.Mail, this); }
        }

        #endregion




        public object GetSettingsData()
        {
            throw new NotImplementedException();
        }
    }
}
